package com.itheima.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.pojo.Order;
import com.itheima.common.vo.OrderVO;
import com.itheima.order.mapper.OrderMapper;
import com.itheima.order.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private Environment environment;

    @Override
    public List<OrderVO> findOrderByUsername(String username) throws InterruptedException {
        String property = environment.getProperty("server.port");
        System.out.println(property);

        //模拟延迟三秒
        //Thread.sleep(3000);

        //int i=1/0;

        //根据用户名查询对应的订单集合
        List<Order> orders = list(new QueryWrapper<Order>().eq("username", username));

        //将Order集合转为OrderVO集合
        List<OrderVO> orderVOList = orders.stream().map(order -> {
            //将Order转为OrderVO对象
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(order, orderVO);
            return orderVO;
        }).collect(Collectors.toList());

        return orderVOList;
    }
}
